﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using Sale_Model;
using SaleSystem;
using ExtAspNet;

namespace SaleSystem.Analysis
{
    public partial class frm_ProfitList : PageBase
    {
        #region PageEvent
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                WebChartControl1.Visible = false;
                WebChartControl2.Visible = false;
                WebChartControl3.Visible = false;
                this.ddl_Type.DataSource = LinkService.GetAnalyzeType();
                this.ddl_Type.DataBind();
                InitControl();
                GetAndBind();
            }
            
        }
        public void cb_Data_Changed(object sender, EventArgs e)
        {
            if (_iShowType == 1)
                PageContext.RegisterStartupScript("GetPosition();");
            else
                PageContext.RegisterStartupScript("GetPosition2();");
                

        }

        public void rbl_Type_SelectedIndexChanged(object sender, EventArgs e)
        {

            string strTip = "";
            if (this.ddl_Type.SelectedValue == "1")
                strTip = DateTime.Parse(this.dp_StartTime.Text).ToString("yyyy年") + "至" + DateTime.Parse(this.dp_StartTime.Text).ToString("yyyy年");
            else if (this.ddl_Type.SelectedValue == "2")
                strTip = DateTime.Parse(this.dp_StartTime.Text).ToString("yyyy年MM月") + "至" + DateTime.Parse(this.dp_EndTime.Text).ToString("yyyy年MM月");
            else
                strTip = DateTime.Parse(this.dp_StartTime.Text).ToString("yyyy年MM月dd日") + "至" + DateTime.Parse(this.dp_EndTime.Text).ToString("yyyy年MM月dd日");
            WebChartControl2.Visible = false;
            WebChartControl1.Visible = false;
            WebChartControl3.Visible = false;


            DataTable dt1 = dtData;
            if (!dt1.Columns.Contains("ShortDate"))
            {

                dt1.Columns.Add("ShortDate");
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    if (this.ddl_Type.SelectedValue == "1")
                    {
                        dtData.Rows[i]["ShortDate"] = dtData.Rows[i]["TimeSpin"];
                    }
                    else if (this.ddl_Type.SelectedValue == "2")
                    {
                        dtData.Rows[i]["ShortDate"] = DateTime.Parse(ValueHandler.GetStringDateValue(dtData.Rows[i]["TimeSpin"])).ToString("yyyy-MM"); 
                    }
                    else if (this.ddl_Type.SelectedValue == "3")
                    {
                        dtData.Rows[i]["ShortDate"] = DateTime.Parse(ValueHandler.GetStringDateValue(dtData.Rows[i]["TimeSpin"])).ToString("dd"); 
 
                    }

                }
            }


            if (rbl_Type.SelectedValue == "1")
            {
                GetChart(WebChartControl2, "ShortDate", rbl_Item.SelectedValue, dtData, this.ddl_Type.SelectedText + "(" + strTip + ")" + rbl_Item.SelectedItem.Text + "分析", this.rbl_Item.SelectedItem.Text);
                WebChartControl2.Visible = true;
                
            }
            else if (rbl_Type.SelectedValue == "2")
            {
                GetPie(WebChartControl1, dtData, "ShortDate", rbl_Item.SelectedValue, this.ddl_Type.SelectedText + "(" + strTip + ")" + rbl_Item.SelectedItem.Text + "分析");

                WebChartControl1.Visible = true;
            }
            else if (rbl_Type.SelectedValue == "3")
            {


                GetPie(WebChartControl3, dtData, "ShortDate", rbl_Item.SelectedValue, this.ddl_Type.SelectedText + "(" + strTip + ")" + rbl_Item.SelectedItem.Text + "分析");  

                WebChartControl3.Visible = true;               
            }
            if (this._iShowType == 1)
            {
                PageContext.RegisterStartupScript("GetPosition();");            
            }
        }

        protected void gv_List_PageIndexChange(object sender, GridPageEventArgs e)
        {
            gv_List.PageIndex = e.NewPageIndex;
        }

 
        protected void bnAnalyze_Click(object sender, EventArgs e)
        { 
            GetAndBind();
        }
        protected void bn_Analyze1_Click(object sender, EventArgs e)
        {
           
            GetAndBind();
        }
        protected void ddl_Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (this.ddl_Type.SelectedValue == "1")
            //{
            //    this.dp_EndTime.DateFormatString = "yyyy";
            //    this.dp_StartTime.DateFormatString = "yyyy";
            //}
            //else if (this.ddl_Type.SelectedValue == "2")
            //{
            //    this.dp_EndTime.DateFormatString = "yyyy-MM";
            //    this.dp_EndTime.DateFormatString = "yyyy-MM";
            //}
            //else if (this.ddl_Type.SelectedValue == "3")
            //{
            //    this.dp_EndTime.DateFormatString = "yyyy-MM-dd";
            //    this.dp_EndTime.DateFormatString = "yyyy-MM-dd";
            //}
            InitControl();
        }
        #endregion
        #region Method
        public void GetAndBind()
        {
            FormatGrid(gv_List);
            DataTable dt= Manager.GetList(ValueHandler.GetIntNumberValue(this.ddl_Type.SelectedValue),ValueHandler.GetStringShortDateValue( this.dp_StartTime.Text), ValueHandler.GetStringShortDateValue( this.dp_EndTime.Text));
            this.gv_List.DataSource = dt;
            this.gv_List.DataBind();
            dtData = dt;

            rbl_Type_SelectedIndexChanged(null, null);

            
        }
        private void InitControl()
        {
            if (this.ddl_Type.SelectedValue == "1")
            {
                dp_StartTime.Text = DateTime.Parse( (System.DateTime.Now.Year - 5).ToString() + "-01-01").ToString("yyyy-MM-dd");
                dp_EndTime.Text = DateTime.Parse( (System.DateTime.Now.Year + 5).ToString() + "-12-31").ToString("yyyy-MM-dd");
            }
            else if (this.ddl_Type.SelectedValue == "2")
            {
                dp_StartTime.Text = DateTime.Parse((System.DateTime.Now.Year).ToString() + "-01-01").ToString("yyyy-MM-dd");
                dp_EndTime.Text = DateTime.Parse((System.DateTime.Now.Year).ToString() + "-12-31").ToString("yyyy-MM-dd");
            }
            else if (this.ddl_Type.SelectedValue == "3")
            {
                dp_StartTime.Text = System.DateTime.Now.ToString("yyyy-MM-01");
                dp_EndTime.Text = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-") + DateTime.DaysInMonth(System.DateTime.Now.Year, System.DateTime.Now.Month).ToString()).ToString("yyyy-MM-dd");
                
            }

        }
        #endregion
        #region Property
        AnalysisOp Manager = new AnalysisOp();
        private int _iShowType
        {
            get
            {
                if (cb_Data.Checked)
                    return 1;
                else
                    return 0;
                 
            } 
        }
        public DataTable dtData
        {
            get
            {
                return (DataTable)ViewState["dtData"];
            }
            set
            {
                ViewState["dtData"] = value;
            }
        }
        #endregion
    }
}
